package com.cardone.platform.authority.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.authority.dto.UsGroupRoleDto;
import com.google.common.collect.Maps;

/**
 * 用户组与角色
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class UsGroupRoleJdbcDao implements UsGroupRoleDao {
	@Override
	public int deleteByIds(final UsGroupRoleDto deleteUsGroupRole) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteUsGroupRole.getAttrs())) {
			model.putAll(deleteUsGroupRole.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteUsGroupRole.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public int deleteByRoleIds(final String roleIds) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put("roleIds", roleIds);

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByRoleIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final UsGroupRoleDto findUsGroupRole) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsGroupRole, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findUsGroupRole, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findUsGroupRole.getAttrs())) {
			model.putAll(findUsGroupRole.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final UsGroupRoleDto findUsGroupRole) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsGroupRole);

		final Model model = ModelUtils.newModel(new ModelArgs(findUsGroupRole, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findUsGroupRole.getAttrs())) {
			model.putAll(findUsGroupRole.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final UsGroupRoleDto findListUsGroupRole) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListUsGroupRole).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListUsGroupRole.getAttrs())) {
			model.putAll(findListUsGroupRole.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final UsGroupRoleDto insertUsGroupRole) {
		if (StringUtils.isBlank(insertUsGroupRole.getId())) {
			insertUsGroupRole.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertUsGroupRole));

		if (MapUtils.isNotEmpty(insertUsGroupRole.getAttrs())) {
			model.putAll(insertUsGroupRole.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final UsGroupRoleDto paginationUsGroupRole) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationUsGroupRole).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationUsGroupRole.getAttrs())) {
			model.putAll(paginationUsGroupRole.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationUsGroupRole), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final UsGroupRoleDto readUsGroupRole) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readUsGroupRole, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readUsGroupRole.getAttrs())) {
			model.putAll(readUsGroupRole.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readUsGroupRole);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readUsGroupRole, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final UsGroupRoleDto saveUsGroupRole) {
		final P oldUsGroupRole = this.findByCodeOrId(mappedClass, saveUsGroupRole);

		if (oldUsGroupRole == null) {
			this.insertByCode(saveUsGroupRole);

			return this.findByCodeOrId(mappedClass, saveUsGroupRole);
		}

		final int updateCount = this.updateByCode(saveUsGroupRole, oldUsGroupRole);

		if (updateCount < 1) {
			return oldUsGroupRole;
		}

		return this.findByCodeOrId(mappedClass, saveUsGroupRole);
	}

	@Override
	public int updateByCode(final UsGroupRoleDto updateUsGroupRole) {
		final UsGroupRoleDto oldUsGroupRole = this.findByCodeOrId(UsGroupRoleDto.class, updateUsGroupRole);

		return this.updateByCode(updateUsGroupRole, oldUsGroupRole);
	}

	private <P> int updateByCode(final UsGroupRoleDto updateUsGroupRole, final P oldUsGroupRole) {
		if (oldUsGroupRole == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateUsGroupRole);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateUsGroupRole, oldUsGroupRole, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldUsGroupRole, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldUsGroupRole, updateUsGroupRole, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateUsGroupRole, updateProperties), new ModelArgs(updateUsGroupRole, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateUsGroupRole.getAttrs())) {
			model.putAll(updateUsGroupRole.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}